﻿using System;
using System.Collections;
using System.Collections.Generic;
using RayDen.Library.Components.Surface;

namespace RayDen.Library.Components
{
    public abstract class MaterialLibraryBase<TBsdf> : IMaterialLibraryBase<TBsdf>
        where TBsdf:ISurfaceMaterial
    {
        protected Dictionary<string, TBsdf> surfMats;
        protected TBsdf @default;

        protected MaterialLibraryBase(TBsdf df) {
            surfMats = new Dictionary<string, TBsdf>();
            new Dictionary<string, string[]>();
            @default = df;
        }

        public TBsdf GetSurfMat(string name) {
            TBsdf res;
            return surfMats.TryGetValue(name??"default", out res) ? 
                res : 
                @default;
        }

        public void Replace(string name, TBsdf bsdf)
        {
            if (bsdf == null)
            {
                throw new ArgumentNullException("bsdf");
            }
            if (!surfMats.ContainsKey(name.ToLower()))
            {
                return;
                throw new ArgumentException("name");
            }

            surfMats[name.ToLower()] = bsdf;
        }

        public IEnumerable<TBsdf> Materials
        {
            get { return surfMats.Values; }
        }

        public IEnumerator<TBsdf> GetEnumerator()
        {
            return this.surfMats.Values.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }


        public abstract void Populate(Entity.Scene.MaterialInfo[] mats);    
    }
}